package com.caopeng.achievementmanagement.controller;

import com.caopeng.achievementmanagement.mapper.AchievementMapper;
import com.caopeng.achievementmanagement.pojo.Achievement;
import com.caopeng.achievementmanagement.pojo.User;
import com.caopeng.achievementmanagement.service.AchievementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Crescent_P
 * @date 2021-12-05 9:35
 */
@Controller
public class IndexController {
    @Autowired
    private AchievementService achievementService;

    // 主页
    @GetMapping("/")
    public String toIndex(
            @RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
            Model model){
        PageHelper.startPage(pagenum, 5);
        // 所有的成果
        List<Achievement> allAchievements = achievementService.getAllAchievements();
        //得到分页结果对象
        PageInfo pageInfo = new PageInfo(allAchievements);
        model.addAttribute("pageInfo", pageInfo);
        return "index";
    }

    // 注销
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:toLogin";
    }

    // 跳转到个人中心页
    @GetMapping("/toPersonalInfo")
    public String toPersonalInfo(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
                                 Model model,
                                 HttpSession session){
        PageHelper.startPage(pagenum, 5);
        // 先得到userid
        User user = (User) session.getAttribute("user");
        // 个人的成果
        List<Achievement> allAchievements = achievementService.getAchievementsByUserID(user.getId());
        //得到分页结果对象
        PageInfo pageInfo = new PageInfo(allAchievements);
        model.addAttribute("pageInfo", pageInfo);
        return "personal_Info";
    }

    // 跳转到上传页面
    @GetMapping("/toUpload")
    public String toUpload(){
        return "page-upload";
    }

    // 跳转到确认删除页面
    @GetMapping("/toDelete/{id}")
    public String toDelete(@PathVariable long id,
                           HttpSession session){
        session.setAttribute("deleteId",id);
        return "page-delete";
    }

    // 跳转到成果修改页面
    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable long id,
                           Model model){
        Achievement achievement = achievementService.getAchievementById(id);

        model.addAttribute("achievement",achievement);
        return "page-update";
    }

    // 跳转到详情页
    @GetMapping("/toDetails/{id}")
    public String toDetails(@PathVariable long id,
                            Model model){
        Achievement achievement = achievementService.getAchievementById(id);
        model.addAttribute("achievement",achievement);
        return "page-details";
    }

    // 搜索
    @PostMapping("/search")
    public String searchAll(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
                            Model model,
                            String title){
        PageHelper.startPage(pagenum, 5);
        // 所有的成果
        List<Achievement> allAchievements = achievementService.searchByTitle(title);
        //得到分页结果对象
        PageInfo pageInfo = new PageInfo(allAchievements);
        model.addAttribute("pageInfo", pageInfo);
        return "index";
    }

    // 跳转到个人中心页
    @PostMapping("/searchByUser")
    public String searchByUser(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
                                 Model model,
                                 HttpSession session,
                                 String title){
        PageHelper.startPage(pagenum, 5);
        // 先得到userid
        User user = (User) session.getAttribute("user");
        // 个人的成果
        List<Achievement> allAchievements = achievementService.searchByTitleAndUser(user.getId(),title);
        //得到分页结果对象
        PageInfo pageInfo = new PageInfo(allAchievements);
        model.addAttribute("pageInfo", pageInfo);
        return "personal_Info";
    }




}
